package io.shdxhl.simplerpc.code.netty.strategy.impl;

import io.shdxhl.simplerpc.client.provider.ServiceProvider;
import io.shdxhl.simplerpc.code.annotation.SimpleLoadBalance;
import io.shdxhl.simplerpc.code.netty.strategy.LoadBalanceStrategy;
import io.shdxhl.simplerpc.code.utils.IPUtil;

import java.util.List;

/**
 * Hash负载均衡
 */
@SimpleLoadBalance(strategy = "hash")
public class HashLoadBalanceStrategy implements LoadBalanceStrategy {
    static String ip= IPUtil.getRealIp();
    @Override
    public ServiceProvider select(List<ServiceProvider> serviceProviders) {
        int hashCode = ip.hashCode();
        int abs = Math.abs(hashCode % serviceProviders.size());
        return serviceProviders.get(abs);
    }
}
